<template>
  <div>
    <button @click="sendMessageToWorker">开始计算</button>
    <input placeholder="请输入内容">
    {{result}}
  </div>
</template>

<script setup>
import {inject, ref} from 'vue';

const worker = ref(null);
const message = ref([]);
const result = ref(0)
// 创建并初始化worker
worker.value = new Worker(new URL('@/workers/mainWorker.js', import.meta.url));
console.log(inject('message'))
// 发送消息到worker
function sendMessageToWorker() {
  worker.value.postMessage({ data:  JSON.stringify(message.value) });
  worker.value.onmessage = (e) => {
    result.value = e.data.result;
    // worker.value.terminate() // worker 销毁
  };
}
</script>